{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d00fc083",
   "metadata": {
    "papermill": {
     "duration": 0.016307,
     "end_time": "2022-01-10T17:05:19.160432",
     "exception": false,
     "start_time": "2022-01-10T17:05:19.144125",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "# Input Webcam"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "645dc2c4",
   "metadata": {
    "papermill": {
     "duration": 0.015642,
     "end_time": "2022-01-10T17:05:19.193671",
     "exception": false,
     "start_time": "2022-01-10T17:05:19.178029",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "Creates video stream from webcam and forwards each frame as PNG one by one to http endpoint via POST"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "166a39dd-a553-4b2b-be0a-0e6e1fdab6fe",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\"\"\"\n",
    "os.environ['create_image']='True'\n",
    "os.environ['repository']='romeokienzler'\n",
    "os.environ['version']='0.14'\n",
    "\"\"\"\n",
    "os.environ['install_requirements']='True'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5ebf4468-2a07-4938-a090-704770bdb762",
   "metadata": {
    "scrolled": true,
    "tags": []
   },
   "outputs": [],
   "source": [
    "if bool(os.environ.get('create_image',False)):\n",
    "    docker_file=\"\"\"\n",
    "    FROM registry.access.redhat.com/ubi8/python-39\n",
    "    RUN pip install ipython nbformat opencv-python\n",
    "    ADD image-endpoint.ipynb /\n",
    "    ENTRYPOINT [\"ipython\",\"/image-endpoint.ipynb\",\"> /tmp/component.log\",\"2> /tmp/component.err\"]\n",
    "    \"\"\"\n",
    "    with open(\"Dockerfile\", \"w\") as text_file:\n",
    "        text_file.write(docker_file)\n",
    "\n",
    "    !docker build -t claimed-predict-image-endpoint:`echo $version` .\n",
    "    !docker tag claimed-predict-image-endpoint:`echo $version` `echo $repository`/claimed-predict-image-endpoint:`echo $version`\n",
    "    !docker push `echo $repository`/claimed-predict-image-endpoint:`echo $version`\n",
    "elif bool(os.environ.get('install_requirements',False)):\n",
    "    !pip install opencv-python"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "054a358d",
   "metadata": {
    "papermill": {
     "duration": 0.02608,
     "end_time": "2022-01-10T17:05:21.005692",
     "exception": false,
     "start_time": "2022-01-10T17:05:20.979612",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "import cv2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cca6e990-40d8-439d-9205-bcfaf2ce82b2",
   "metadata": {},
   "outputs": [],
   "source": [
    "from __future__ import print_function\n",
    "import requests\n",
    "import json\n",
    "import cv2\n",
    "\n",
    "vid = cv2.VideoCapture(0)\n",
    "\n",
    "while(True):\n",
    "      \n",
    "    # Capture the video frame\n",
    "    # by frame\n",
    "    ret, frame = vid.read()\n",
    "    if not ret:\n",
    "        print('err')\n",
    "        break\n",
    "  \n",
    "    _, img_encoded = cv2.imencode('.png', frame)\n",
    "    \n",
    "    content_type = 'multipart/form-data'\n",
    "    headers = {'content-type': content_type}\n",
    "      \n",
    "    response = requests.post('http://127.0.0.1:8080/process-labels', data=img_encoded.tostring(), headers=headers)\n",
    "# decode response\n",
    "    print(response.text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cd0b2746-85c6-4748-b290-c76f30adf248",
   "metadata": {},
   "outputs": [],
   "source": [
    "vid.release()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b595c0b6-4722-4dd9-8c26-8a5f8df24601",
   "metadata": {},
   "outputs": [],
   "source": [
    "vid = cv2.VideoCapture(0)\n",
    "  \n",
    "while(True):\n",
    "      \n",
    "    # Capture the video frame\n",
    "    # by frame\n",
    "    ret, frame = vid.read()\n",
    "  \n",
    "    # Display the resulting frame\n",
    "    cv2.imshow('frame', frame)\n",
    "      \n",
    "    # the 'q' button is set as the\n",
    "    # quitting button you may use any\n",
    "    # desired button of your choice\n",
    "    if cv2.waitKey(1) & 0xFF == ord('q'):\n",
    "        break\n",
    "  \n",
    "# After the loop release the cap object\n",
    "vid.release()\n",
    "# Destroy all the windows\n",
    "cv2.destroyAllWindows()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  },
  "papermill": {
   "default_parameters": {},
   "duration": 41.725932,
   "end_time": "2022-01-10T17:05:59.665500",
   "environment_variables": {},
   "exception": null,
   "input_path": "/home/romeokienzler/gitco/claimed/component-library/input/input-url.ipynb",
   "output_path": "/home/romeokienzler/gitco/claimed/component-library/input/input-url.ipynb",
   "parameters": {},
   "start_time": "2022-01-10T17:05:17.939568",
   "version": "2.3.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
